package logic;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Vector;
import main.GameDef;


public class Piece implements Manipulable, GameDef{
/*
 * Atributes
 */	
	final public static int INACTIVE	= 0;
	final public static int ACTIVE		= 1;
	public Rectangle area;
	public Vector <Block>blocks;
	public int type;
	public int numBlocks;
	public int status;
/*
 * Constructors
 */
	public Piece(int _numBlocks, int pieceType){
		type = pieceType;
		numBlocks = _numBlocks;
		blocks = new Vector <Block>(0);
		status = ACTIVE;
	}
	
/*
 * Setters
 */

	public boolean isHit(Point point) {
		for(int i = 0; i < blocks.size(); i++){
			if(blocks.get(i).area.contains(point)){
				blocks.get(i).status = Block.DAMAGED;
				return true;
			}
		}
		return false;
	}
	
	public int remainingBlocks(){
		int cant = 0;
		
		for(int i = 0; i < blocks.size(); i++){
			if(blocks.get(i).status == Block.HEALTHY){
				cant++;
			}
		}
		
		return cant;
	}
	
	public boolean repairBlock(Point position){
		for(int i = 0; i < blocks.size(); i++){
			if(blocks.get(i).status == Block.DAMAGED && blocks.get(i).area.contains(position) && status == ACTIVE){
				blocks.get(i).status = Block.HEALTHY;
				return true;
			}
		}
		return false;
	}
	
	public void arrangeBlocks(Rectangle _area, boolean vertical){
		area = _area;
		int pieceWidth = 0;
		Point pos = area.getLocation();
		
		switch(type){
			case ID_INTERIOR:
			case ID_HOUSE:
				pieceWidth = 1;
				break;
			case ID_CATAPULT:
				pieceWidth = 1;
				break;
			case ID_CANNON:
				pieceWidth = 2;
				break;
			case ID_ARMORY:
				pieceWidth = 3;
				break;
		}
		pos.translate(-(pos.x % BLOCK_WIDTH), -(pos.y % BLOCK_HEIGHT));
		for(int i = 0; i < numBlocks; i++){
			Point blockPos = pos.getLocation();
			if(vertical){
				blockPos.translate((i % pieceWidth) * BLOCK_WIDTH, (i / pieceWidth) * BLOCK_HEIGHT);
			} else {
				blockPos.translate((i / pieceWidth) * BLOCK_WIDTH, (i % pieceWidth) * BLOCK_HEIGHT);	
			}
			blocks.add(new Block(blockPos));
		}
	}
}
